//
//  HomepageActionTableViewCell.m
//  KuWoCan
//
//  Created by 周小宏 on 2017/4/20.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "HomepageActionTableViewCell.h"

#define ACTION_VIEW_TAG 1578765283

@implementation HomepageActionTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)congfigActionCellWithPlist:(NSString *)plistName {

    for (UIView *view in self.contentView.subviews) {
        [view removeFromSuperview];
    }
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:plistName ofType:@"plist"];
    NSArray *plistArr = [[NSArray alloc] initWithContentsOfFile:filePath];
    [plistArr enumerateObjectsUsingBlock:^(NSDictionary *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *backgroundView;
        if (idx < 2) {
            backgroundView = [[UIView alloc] initWithFrame:CGRectMake(10 * screenW_6, 7.5 *screenW_6 + (127.5 * screenW_6 * idx), 150 * screenW_6, 122.5 * screenW_6)];
            backgroundView.backgroundColor = [UIColor colorWithHexString:obj[@"backgroundColor"]];

            UIImageView *imageV = [[UIImageView alloc] initWithImage:kImg(obj[@"image"])];
            imageV.frame = CGRectMake(55.5 * screenW_6, 32.5 * screenW_6, 45 * screenW_6, 45 * screenW_6);
            [backgroundView addSubview:imageV];

            UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(imageV.frame) + 15 * screenW_6, backgroundView.width, 16 * screenW_6)];
            title.textAlignment = NSTextAlignmentCenter;
            title.text = obj[@"title"];
            title.font = KwFont(16 * screenW_6);
            title.textColor = [UIColor whiteColor];
            [backgroundView addSubview:title];
        } else {
            backgroundView = [[UIView alloc] initWithFrame:CGRectMake(SCREENW - 210 * screenW_6, 7.5 *screenW_6 + (85 * screenW_6 * (idx - 2)), 200 * screenW_6, 80 *screenW_6)];
            backgroundView.backgroundColor = [UIColor colorWithHexString:obj[@"backgroundColor"]];

            UIImageView *imageV = [[UIImageView alloc] initWithImage:kImg(obj[@"image"])];
            imageV.frame = CGRectMake(25 * screenW_6, 80 * screenW_6 / 2 - (45 * screenW_6 / 2), 45 *screenW_6, 45 *screenW_6);
            [backgroundView addSubview:imageV];

            UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageV.frame), 80 * screenW_6 / 2 - (16 * screenW_6 / 2), 200 *screenW_6 - (CGRectGetMaxX(imageV.frame) + 12 *screenW_6), 16 * screenW_6)];
            title.text = obj[@"title"];
            title.font = KwFont(16 * screenW_6);
            title.textColor = [UIColor whiteColor];
            [backgroundView addSubview:title];

            if (idx == 3) {
                title.frame = CGRectMake(35 * screenW_6, 22.5 * screenW_6, 200 *screenW_6 - 119 * screenW_6, 80 * screenW_6);
                title.numberOfLines = 2;
                [title sizeToFit];
                imageV.frame = CGRectMake(CGRectGetMaxX(title.frame) + 22 * screenW_6, 80 * screenW_6 / 2 - (45 * screenW_6 / 2), 45 *screenW_6, 45 *screenW_6);
            }
        }
        backgroundView.tag = idx + ACTION_VIEW_TAG;
        backgroundView.layer.cornerRadius = 4 * screenW_6;
        backgroundView.layer.masksToBounds = YES;
        [self.contentView addSubview:backgroundView];

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(freeApply:)];
        [backgroundView addGestureRecognizer:tap];
    }];
}

- (void)freeApply:(UITapGestureRecognizer *)recognizer {
    UIView *view = recognizer.view;
    ApplyType type = view.tag - ACTION_VIEW_TAG;
    switch (view.tag - ACTION_VIEW_TAG) {
        case 0:
            type = ApplyTypeOrderManage;
            break;
        case 1:
            type = ApplyTypeEntryFeeApply;
            break;
        case 2:
            type = ApplyTypePromotionApply;
            break;
        case 3:
            type = ApplyTypeProductDisplayFeeApply;
            break;
        case 4:
            type = ApplyTypeOtherFeeApply;
            break;
        default:
            break;
    }
    if (self.actionViewCliecked) {
        self.actionViewCliecked(type);
    }
}

@end
